{
 "cells": [
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# [Create a collection](https://milvus.io/docs/v2.3.0-beta/create_collection.md)\n",
    "\n",
    "🎁🎁🎁🎁🎁🎁🎁🎁🎁🎁🎁🎁🎁🎁\n",
    "\n",
    "> This topic describes how to create a collection in Milvus.\n",
    "\n",
    "A collection consists of one or more partitions. While creating a new collection, Milvus creates a default partition _default. See [Glossary](https://milvus.io/docs/v2.3.0-beta/glossary.md#Collection) - Collection for more information."
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Connect to milvus"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "dotnet_interactive": {
     "language": "csharp"
    },
    "polyglot_notebook": {
     "kernelName": "csharp"
    },
    "vscode": {
     "languageId": "polyglot-notebook"
    }
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div><div></div><div></div><div><strong>Installed Packages</strong><ul><li><span>Milvus.Client, 2.2.2-preview.2</span></li></ul></div></div>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/html": [
       "<details open=\"open\" class=\"dni-treeview\"><summary><span class=\"dni-code-hint\"><code>{MilvusClient:localhost:19530}</code></span></summary><div><table><thead><tr></tr></thead><tbody><tr><td>Address</td><td><div class=\"dni-plaintext\"><pre>localhost:19530</pre></div></td></tr></tbody></table></div></details><style>\r\n",
       ".dni-code-hint {\r\n",
       "    font-style: italic;\r\n",
       "    overflow: hidden;\r\n",
       "    white-space: nowrap;\r\n",
       "}\r\n",
       ".dni-treeview {\r\n",
       "    white-space: nowrap;\r\n",
       "}\r\n",
       ".dni-treeview td {\r\n",
       "    vertical-align: top;\r\n",
       "    text-align: start;\r\n",
       "}\r\n",
       "details.dni-treeview {\r\n",
       "    padding-left: 1em;\r\n",
       "}\r\n",
       "table td {\r\n",
       "    text-align: start;\r\n",
       "}\r\n",
       "table tr { \r\n",
       "    vertical-align: top; \r\n",
       "    margin: 0em 0px;\r\n",
       "}\r\n",
       "table tr td pre \r\n",
       "{ \r\n",
       "    vertical-align: top !important; \r\n",
       "    margin: 0em 0px !important;\r\n",
       "} \r\n",
       "table th {\r\n",
       "    text-align: start;\r\n",
       "}\r\n",
       "</style>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "#r \"nuget: Milvus.Client, 2.2.2-preview.2\"\n",
    "#!import config/Settings.cs\n",
    "\n",
    "using Milvus.Client;\n",
    "using InteractiveKernel = Microsoft.DotNet.Interactive.Kernel;\n",
    "\n",
    "//Connect to milvus\n",
    "(string endpoint, int port, string userName, string password) = Settings.LoadFromFile();\n",
    "\n",
    "MilvusClient milvusClient = default;\n",
    "\n",
    "milvusClient = new MilvusClient(endpoint, port, userName, password, null);\n",
    "milvusClient"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Create a collection\n",
    "\n",
    "First, prepare necessary parameters, including field schema, collection schema, and collection name.\n",
    "\n",
    "> The collection to create must contain a primary key field and a vector field. INT64 and String are supported data type on primary key field."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "dotnet_interactive": {
     "language": "csharp"
    },
    "polyglot_notebook": {
     "kernelName": "csharp"
    },
    "vscode": {
     "languageId": "polyglot-notebook"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Drop collection book\r\n"
     ]
    }
   ],
   "source": [
    "using System.Diagnostics;\n",
    "\n",
    "string collectionName = \"book\";\n",
    "MilvusCollection collection = milvusClient.GetCollection(collectionName);\n",
    "\n",
    "//Check if this collection exists\n",
    "var hasCollection = await milvusClient.HasCollectionAsync(collectionName);\n",
    "\n",
    "if(hasCollection){\n",
    "    await collection.DropAsync();\n",
    "    Console.WriteLine(\"Drop collection {0}\",collectionName);\n",
    "}\n",
    "\n",
    "await milvusClient.CreateCollectionAsync(\n",
    "            collectionName,\n",
    "            new[] {\n",
    "                FieldSchema.Create<long>(\"book_id\", isPrimaryKey:true),\n",
    "                FieldSchema.Create<long>(\"word_count\"),\n",
    "                FieldSchema.CreateVarchar(\"book_name\", 256),\n",
    "                FieldSchema.CreateFloatVector(\"book_intro\", 2L)\n",
    "            }\n",
    "        );"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Collection info\n",
    "📰📰📰📰📰📰📰📰📰📰📰📰📰\n",
    "\n",
    "Now, we created a collection. We can use DescribeCollectionAsync to show more details about this collection."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "dotnet_interactive": {
     "language": "csharp"
    },
    "polyglot_notebook": {
     "kernelName": "csharp"
    },
    "vscode": {
     "languageId": "polyglot-notebook"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "CollectionName:book, CollectionId:443732094175944322, ConsistencyLevel:Session, CreatedTime:1970/1/1 1:47:37\r\n"
     ]
    }
   ],
   "source": [
    "//Show collection iformation\n",
    "MilvusCollectionDescription collectionInfo = await collection.DescribeAsync();\n",
    "\n",
    "Console.WriteLine(\"CollectionName:{0}, CollectionId:{1}, ConsistencyLevel:{2}, CreatedTime:{3}\",\n",
    "    collectionInfo.CollectionName,\n",
    "    collectionInfo.CollectionId,\n",
    "    collectionInfo.ConsistencyLevel,\n",
    "    MilvusTimestampUtils.ToDateTime(collectionInfo.CreationTimestamp));"
   ]
  }
 ],
 "metadata": {
  "language_info": {
   "name": "python"
  },
  "orig_nbformat": 4
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
